﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using PDAMain.DAL;
using System.Windows.Forms;
using PDAMain.Forms.ReadingWatermeter;
using PDAMain.Interface;
//using PDAMain.BLL;

namespace PDAMain.Forms.ReadingWatermeter
{
    public partial class WaterRecordSel : Form
    {
        public WaterRecordSel()
        {
            InitializeComponent();
        }

        private void menuSearch_Click(object sender, EventArgs e)
        {
            if (!NetWorkState.checkWifiState() && !NetWorkState.checkGprsState())
            {
                MessageBox.Show("无法连接网络");
                return;
            }
            string MeterId = txtAmterID.Text.ToString().Trim();
            string BuildingId = txtroomID.Text.ToString().Trim();
            string timeStart = TPBeginDT.Value.ToString("yyyy/MM/dd hh:mm:ss");
            string timeEnd = TPEndDT.Value.ToString("yyyy/MM/dd hh:mm:ss");

            DataSet ds = DataHelper.ReadingWatermeterDao.select(MeterId, BuildingId, timeStart, timeEnd);
            /*DataSet ds = null;
            try
            {


                pdaWebReference.PdaService pda = PublicMethods.getobject();
                string Water = pda.queryWatermeterReadingWorkOrder(WaterID, BeginDt, EndDt, Identity.dutyPerson);
                if (Water != string.Empty)
                {
                    ds = PublicMethods.convertToDataSet(Water);

                }
            }
            catch
            {
                string strsql = " SELECT WM_METER_ID as meterID,READMONTH as month,PRE_DEGREE as lastDegree,READDEGREE as degree,DISTRICT_ID,"
                              + "BUILDING_ID,APARMENT_ID,READINGTIME FROM WATERMETER where WM_METER_ID like '%" + WaterID + "%' ORDER BY READDEGREE DESC";
                ds = PublicMethods.SelectData(strsql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (Convert.ToDateTime(ds.Tables[0].Rows[index]["READINGTIME"]) < Convert.ToDateTime(BeginDt) || Convert.ToDateTime(ds.Tables[0].Rows[index]["READINGTIME"]) > Convert.ToDateTime(EndDt))
                        {
                            ds.Tables[0].Rows.RemoveAt(index);
                        }
                    }
                }
            }*/
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                dgWMeterList.Visible = false;
                dgWMeterList.TableStyles.Clear();
                dgWMeterList.Refresh();
                MessageBox.Show("无数据");
                return;
            }

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dgWMeterList.Visible = true;

                DataTable dtmapping = ds.Tables[0];
                dtmapping = Check_Null(dtmapping);
                dgWMeterList.DataSource = dtmapping;
                DataGridTableStyle dts = new DataGridTableStyle();
                dts.MappingName = dtmapping.TableName;

                DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
                col1.HeaderText = "水表号";
                col1.MappingName = "amId";
                col1.Width = 120;
                dts.GridColumnStyles.Add(col1);

                DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
                col2.HeaderText = "抄表月份";
                col2.MappingName = "month";
                col2.Width = 64; ;
                dts.GridColumnStyles.Add(col2);

                DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
                col3.HeaderText = "上月读数";
                col3.MappingName = "lastDegree";
                col3.Width = 65;
                dts.GridColumnStyles.Add(col3);


                DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
                col4.HeaderText = "本月读数";
                col4.MappingName = "degree";
                col4.Width = 65;
                dts.GridColumnStyles.Add(col4);

                dgWMeterList.TableStyles.Clear();
                dgWMeterList.TableStyles.Add(dts);
                dgWMeterList.Refresh();

            }
        }

        private DataTable Check_Null(DataTable dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["lastDegree"].ToString() == "")
                {
                    dt.Rows[i]["lastDegree"] = "";
                }
            }
            return dt;
        }

        private void menuBack_Click(object sender, EventArgs e)
        {
            WaterReadingList WRL = new WaterReadingList();
            WRL.Show();
            this.Close();
        }
        /*private void verifySearchData()
        {

            string strBeginDT = string.Empty;
            string strEndDT = string.Empty;

            strBeginDT = TPBeginDT.Text.ToString().Trim();
            strEndDT = TPEndDT.Text.ToString().Trim();
            //對數據進行驗證

            if (!PublicMethods.verifyData(strBeginDT, "date"))
            {
                MessageBox.Show("开始时间有误！");
                return;
            }
            if (!PublicMethods.verifyData(strEndDT, "date"))
            {
                MessageBox.Show("结束时间有误！");
                return;
            }
            DateTime dtBegin = Convert.ToDateTime(strBeginDT);
            DateTime dtEnd = Convert.ToDateTime(strEndDT);
            if (dtBegin > dtEnd)
            {
                MessageBox.Show("结束时间必须大于开始时间！");
                return;
            }
        }*/
     }
}